/**
 * Created by lx on 2017/9/26.
 */
/**
 * Created by lx on 2017/9/25.
 */
angular.module('module.table.operation')
    .controller('TableNumController', TableNumController);
TableNumController.$inject = ['$state', 'TableNumService', 'shopTableList', '$localStorage', 'SetService'];
function TableNumController($state, TableNumService, shopTableList, $localStorage, SetService) {

    var vm = this;

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    vm.openAddOpen = openAddOpen;
    vm.getFocus = getFocus;
    var shopid = $localStorage[environmental + "shopid"];
    $('.modal-backdrop ').hide(); // 初始化
    var shopele = 0;

    SetService.getShopinfo(shopid, function (success) {
        if (success) {
            shopele = success.chargeTisement;
        }
    });

    if (shopTableList != 'undefined') {
        var datalist = [];
        vm.tablelist = shopTableList;
        for (var i = 0; i < vm.tablelist.length; i++) {
            var data = {};
            data['shopid'] = shopid;
            data['tableid'] = vm.tablelist[i].tableid;
            data['groupid'] = vm.tablelist[i].groupid;
            datalist.push(data);
        }
        TableNumService.getalldetail(datalist).$promise.then(function (sucF) {
            var listtable = sucF;
            for (var j = 0; j < listtable.length; j++) {
                for (var k = 0; k < vm.tablelist.length; k++) {
                    if (vm.tablelist[k].tableid == listtable[j].tableid) {
                        getStatus(listtable[j], vm.tablelist[k])
                    }
                }
            }
        }, function (error) {
        })
    } else {
        vm.tablelist = [];
    }

    function openAddOpen() {
        $('#tablenumModal').modal('show');
        $("#cancelbtn").addClass("detail_btn_active");
        $("#surebtn").removeClass("detail_btn_active");
        vm.addGroup = function (name) {
            checkedBtn("surebtn");
            if (!name) {
                blinklink(5, $("#inputname"));
                return;
            }

            var surebtn = $("#surebtn");
            surebtn.attr("disabled", true);
            var shoptablenum = {
                shopid: shopid,
                tablename: name,
            };

            TableNumService.addTableNum(shoptablenum, function (succsedata) {
                vm.tablelist.push(succsedata);
                surebtn.attr("disabled", false);
                $('#tablenumModal').modal('hide');
                $state.go('tabledetail', {tableid: succsedata.tableid})
            }, function (error) {
                surebtn.attr("disabled", false);
            })

        }
    }

    function checkedBtn(domid) {
        if (domid = "surebtn") {
            $("#" + domid).addClass("detail_btn_active");
            $("#cancelbtn").removeClass("detail_btn_active");
        } else {
            $("#" + domid).addClass("detail_btn_active");
            $("#surebtn").removeClass("detail_btn_active");
        }
    }

    function getFocus() {
        $("#inputname").css("border-bottom-color", "#a7a7a7");
    }

    function getStatus(ta, ni) {
        ni.msg = '';
        getDevIdByTableId(ni);
        if (!ta.groupname) {
            ni.groupname = '默认';
        } else {
            ni.groupname = ta.groupname;
        }
        if (ta.shopDevice && ta.shopDevice.length > 0) {
            ni.devstatus = ta.shopDevice;
            TableNumService.getShopTableByEle(ni.devstatus[0].devid).$promise.then(function (sucData) {
                if (sucData.electricity < shopele) {
                    ni.text = 0;
                } else {
                    ni.text = 1;
                }

            }, function (error) {
                ni.text = 1;
            })
        } else {
            ni.devstatus = [
                {
                    devid: '暂无设备',
                }
            ];
            ni.text = 1;
        }

    }

    function getDevIdByTableId(table) {
        if (table) {
            TableNumService.getDetailByTableId(shopid, table.tableid, function (sucData) {
                if (sucData.length > 0) {
                    table.status = sucData[0].status;
                } else {
                    table.status = 0;
                }
            }, function (error) {
                table.status = 0;
            }, true);
        }
    }
}